home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / udos / fgrep103 / bm.cpp next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  6.2 KB  |  172 lines

  1. /*******************************************************************************
  2. *                     Boyer-Moore String Matching Algorithm                    *
  3. *                                                                              *
  4. *     Turbo-C version for MS-DOS                                               *
  5. *     adapted from UNIX source code                                            *
  6. *                                                                              *
  7. *     Uses Boyer-Moore search algorithm.                                       *
  8. *                                                                              *
  9. *     For help execute the program without parameters.                         *
  10. *                                                                              *
  11. *     Multiple file specifications may be made on the command line.            *
  12. *                                                                              *
  13. *     This was compiled with the tiny memory model and then EXE2BIN            *
  14. *       was used on the output.  The small memory model could just             *
  15. *       as easily been used.                                                   *
  16. *                                                                              *
  17. *     David Polansky                                                           *
  18. *     3-21-88                                                                  *
  19. *                                                                              *
  20. *******************************************************************************/
  21. #include <stdio.h>
  22. #include <stdlib.h>  /* N2 03-17-91 */
  23. #include <fcntl.h>
  24. #include <string.h>
  25. #include <dir.h>
  26. #include <io.h>
  27. #include <dos.h>
  28. #include <conio.h>  /* N2 03-01-91 */
  29. #include "bm.h"
  30. #include "extern.h"
  31. #include "proto.h"  /* N2 04-05-91 */
  32.  
  33. #define TRUE 1
  34. #define FALSE 0
  35. #define true 1
  36. #define false 0
  37.  
  38. char s_path[80];                    /* source path */
  39. char s_drv[3];                      /* drive */
  40. char s_dir[66];                     /* directory */
  41. char s_file[9];                     /* filename */
  42. char s_ext[5];                      /* file ext */
  43.  
  44. struct ffblk fcb;                   /* where our file names will be stored */
  45. int scrnlines;                      // 11-28-91 count lines displayed
  46. char showfname;                     // flag for filename
  47.  
  48. unsigned _stklen = 6144;
  49.  
  50. int main(int argc, char **argv)                /* N2 06-30-91 */
  51.  {
  52.   char BigBuff[MAXBUFF + 2];
  53.   /* We leave one extra character at the beginning and end of the buffer,
  54.    * but don't tell Execute about it. This is so when someone is
  55.    * scanning the buffer and scans past the end (or beginning)
  56.    * we are still technically in the buffer (picky, but there ARE
  57.    * machines which would complain) */
  58.  
  59.   int ret = 1;     /* return code from Execute */
  60.   int NPats;       /* number of patterns to search for */
  61.   char *FlagPtr;   /* pointer to the flags */
  62.   char c;
  63.   int i;           /* index of argument list */
  64.   int TextFile;    /* file to search */
  65.   struct PattDesc *DescVec[MAXPATS];
  66.   int first_time_through = TRUE;   /* whether to use findfirst or findnext */
  67.   char *PatternFile;
  68.   char *Pattern;
  69.  
  70.   mFlag = TRUE;
  71.   if (argc < 2) PutUsage(); // 11-28-91 this will exit!
  72.   for(i = 1; i < argc; i++)
  73.    {
  74.     if ((argv[i][0] != '-') || (argv[i][1] == '\0')) break; // 11-28-91
  75.     FlagPtr = argv[i];
  76.     while(c = (char)*++FlagPtr)     // while c != \0x0 do *??
  77.      {
  78.       c = tolower(c);         /* 11-28-91 accept upper case switches */
  79.       switch(c)
  80.        {
  81.         case 'm': mFlag = FALSE; break;
  82.         case 'c': cFlag = 1; break;
  83.         case 'f': fFlag = 1; /* read the patterns from a file */
  84.                   PatternFile = argv[++i];
  85.                   break;
  86.         case 'e': eFlag = 1; /* get the patterns from next arg */
  87.                   Pattern = argv[++i];
  88.                   break;
  89.         case 'l': lFlag = 1; break;
  90.         case 'n': nFlag = 1; break;
  91.         case 's': sFlag = 1; break;
  92.         case 'x': xFlag = 1; break;
  93.         case 'h': hFlag = 1; break;
  94.         case 'z': zFlag = 1; break; // 11-28-91
  95.         default:
  96.                   fprintf(stderr,"grep: invalid option: -%c\n",c); // 1.01
  97.                   PutUsage();
  98.                   break;
  99.        }  /* switch */
  100.      }  /* while */
  101.    }
  102.    /* argv[i] now points to patterns */
  103.   if (!fFlag && !eFlag)
  104.    {
  105.     if (i > (argc - 1))
  106.      {
  107.       puts("grep: no pattern specified"); /* 11-28-91 */
  108.       PutUsage();
  109.      }
  110.     else
  111.      {
  112.       NPats = MkDescVec(DescVec,argv[i]);
  113.       ++i;
  114.      }
  115.    }
  116.   else
  117.    if (fFlag)
  118.     {
  119.      NPats = GetPatFile(PatternFile,DescVec);
  120.     }
  121.    else
  122.     if (eFlag)
  123.      {
  124.       NPats = MkDescVec(DescVec,Pattern);
  125.      }
  126.    /* argv[i] now points to first file */
  127.   if (i == argc)
  128.    {
  129.     /* use standard input, could be keyboard */
  130.     ret &= Execute(DescVec,NPats,0,BigBuff+1);
  131.    }
  132.   else
  133.    {
  134.     scrnlines = wherey();               // 11-28-91 where did we start
  135.     if(scrnlines >= 23) scrnlines = 1;  // in case at end of screen
  136.     for ( ;i < argc;i++)
  137.      {
  138.       first_time_through = TRUE;
  139.       fnsplit(argv[i],s_drv,s_dir,s_file,s_ext);      /* split the names */
  140.       while (TRUE)         /* a BREAK will end this loop */
  141.        {
  142.         if (first_time_through)
  143.          {
  144.           if (findfirst(argv[i],&fcb,0) == EOF)
  145.            {
  146.             fprintf(stderr,"No files matching %s\n",argv[i]);
  147.             exit(2);
  148.            }
  149.           first_time_through = FALSE;
  150.          }
  151.         else
  152.          {
  153.           if (findnext(&fcb) == EOF)
  154.            break;      /* no more files to convert */
  155.          }
  156.         FileName = fcb.ff_name; // 11-28-91
  157.         strcpy(s_path,s_drv);
  158.         strcat(s_path,s_dir);
  159.         strcat(s_path,fcb.ff_name);
  160.         TextFile = _open(s_path,O_RDONLY);
  161.         showfname = true; // reset showfname
  162.         ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
  163.         if (sFlag && !ret) exit(0);
  164.         close(TextFile);
  165.        }// while true
  166.      }
  167.    }
  168.   if (cFlag) printf("%d\n",MatchCount);
  169.   exit(0); // was ret
  170.   return 0; // 04-01-92 N2
  171.  } /* main */
  172.